const zl = {}
zl.install = function (Vue, options) {
  // 全局常量
  Vue.prototype.DURATION = 2.5

  // 该形式， vue组件能够使用该方法
  Vue.prototype.$limitLen = (event, len = 128) => {
    if (event.currentTarget.value.length > len) {
      event.currentTarget.value = event.currentTarget.value.substring(0, len - 1)
    }
  }

  Vue.prototype.hasPermission = (store, environmentId, ownPermission) => {
    // 超级管理员
    if (store.state.permissions.admin) {
      return true
    }
    // 某个环境的权限处理
    if (store.state.permissions.namespaces && environmentId && ownPermission) {
      for (let i = 0; i < store.state.permissions.namespaces.length; i++) {
        const ns = store.state.permissions.namespaces[i]
        if (environmentId === ns.id) {
          for (let j = 0; j < ns.permissions.length; j++) {
            const permission = ns.permissions[j]
            if (permission.define === ownPermission) {
              return true
            }
          }
        } else if (environmentId === '1') {
          // 新增环境权限判断
          for (let j = 0; j < ns.roles.length; j++) {
            const role = ns.roles[j]
            if (role.define === '环境管理员角色') {
              return true
            }
          }
        }
      }
    }
    return false
  }
}

export default zl
